﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Generic = System.Collections.Generic;

namespace iPdf
{
    class SplitPage : TataRibbonPage
    {
        #region Fields
        private Dictionary<string, int> showUpTimeDict;
        #endregion

        #region Properties
        protected override string DestinationSuffix
        {
            get
            {
                return ".Part";
            }
        }

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.SplitPageIntroInfo;
            }
        }
        #endregion

        #region Ctor
        public SplitPage()
            : base()
        {
            showUpTimeDict = new Dictionary<string, int>();
            visibleColumns.Add(TataGrid.Password);
            visibleColumns.Add(TataGrid.Destination);
            visibleColumns.Add(TataGrid.From);
            visibleColumns.Add(TataGrid.To);
            visibleColumns.Add(TataGrid.Bookmark);

            actor = new PdfSplitter();
        }
        #endregion

        #region Methods

        protected override ActionItem GetIthActionItem(TataGrid grid, int i)
        {
            SourceGrid.Cells.Cell sourceCell = (SourceGrid.Cells.Cell)grid[i, TataGrid.Source.Index];
            SourceGrid.Cells.Cell destCell = (SourceGrid.Cells.Cell)grid[i, TataGrid.Destination.Index];
            SourceGrid.Cells.Cell fromCell = (SourceGrid.Cells.Cell)grid[i, TataGrid.From.Index];
            SourceGrid.Cells.Cell toCell = (SourceGrid.Cells.Cell)grid[i, TataGrid.To.Index];

            SplitItem item = new SplitItem();
            item.Source = (string)sourceCell.Value;
            item.Password = (PasswordString)grid.GetColumnValueAtRow(i, TataGrid.Password);
            item.Destination = (string)destCell.Value;
            item.Bookmark = (bool)grid.GetColumnValueAtRow(i, TataGrid.Bookmark);
            
            try
            {
                item.From = (int)fromCell.Value;
                item.To = (int)toCell.Value;
            }
            catch
            {
                item.From = 0;
                item.To = 0;
            }

            return item;
        }

        public override string GetDestination(string item, string outputFolder)
        {
             int showUpTime = 1;
             if (showUpTimeDict.ContainsKey(item))
             {
                 showUpTime = showUpTimeDict[item] + 1;
             }
            showUpTimeDict[item] = showUpTime;
            string destination = base.GetDestination(item, outputFolder);
            int lastDotIndex = destination.LastIndexOf(Helper.Dot);

            return destination.Insert(lastDotIndex,  showUpTime.ToString());
        }

        public override void OnTabPageChanged()
        {
            base.OnTabPageChanged();

            if (Global.CurrentPage != this)
            {
                showUpTimeDict.Clear();
            }
        }

        #endregion
    }
}
